// pages/commons/authorize/authorize.js
var app = getApp(); //获取应用实例
const pathLevel = '../../../'; //路径层级
const quote = require(pathLevel + 'utils/quote.config.js') //统一引入文件
const api = quote.api;
const path = quote.paths;
const tools = quote.tools;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        path: path,
        imgUrl: '/src/images/logo.png',
        title: '天天口语秀',
        fromPage: '',
        authSet: '',
        authObjList: {
            'scope.userInfo': '· 获得你的公开信息（头像，昵称等）',
            'scope.record': '· 允许访问你的录音功能',
            'scope.writePhotosAlbum': '· 允许保存照片或视频到你的相册'
        },
        btnText: '去授权'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;

        console.log('打开授权页获取到的参数', options)
        var fromPage = options.fromPage;
        var authSet = options.authSet;
        that.setData({
            fromPage: fromPage,
            authSet: authSet,
            [authSet]: true
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    opensetting: function(e) {
        var that = this;
        var fromPage = that.data.fromPage;

        console.log('e', e)
        var authSetting = e.detail.authSetting
        var authSet = that.data.authSet

        if (!authSetting[authSet]) {
            wx.showModal({
                title: '温馨提示',
                content: '授权未开启，无法继续为你服务！',
                showCancel: true,
                cancelText: '取消',
                cancelColor: "#999",
                confirmText: '开启',
                confirmColor: "#3cc51f",
                success: function(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                            // getoPage(path., 'navigateTo');
                    } else if (res.cancel) {
                        getoPage('index');
                    }
                    return
                }
            })
        } else {
            console.log('授权成功', fromPage, );
            getoPage(fromPage, 'navigateBack');
        }


        function getoPage(pathName, action, delta) {
            switch (action) {
                case 'redirectTo':
                    wx.redirectTo({
                        url: `/${path[pathName]}`,
                        success: function(res) {
                            // success
                        }
                    })
                    break;
                case 'switchTab':
                    wx.switchTab({
                        url: `/${path[pathName]}`,
                        success: function(res) {
                            // success
                        }
                    })
                    break;
                case 'navigateBack':
                    wx.navigateBack({
                        delta: delta || 1
                    })
                    break;
                case "reLaunch":
                    wx.reLaunch({
                        url: `/${path[pathName]}`,
                        success: function(res) {
                            // success
                        }
                    })
                    break;
                default:
                    if (pathName == 'index' || pathName == 'my') {
                        wx.switchTab({
                            url: `/${path[pathName]}`,
                            success: function(res) {
                                // success
                            }
                        })
                    } else {
                        wx.navigateTo({
                            url: `/${path[pathName]}`,
                            success: function(res) {
                                // success
                            }
                        })
                    }
                    break;
            }
        }
    }
})